﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace SPIWareMVVM.Views.Conversions
{
    public class OpCostPerPartConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            //OpCostPerPart = (double)PressOpCost / PartsPerHour_AS 
            if (values.Count() == 2)
            {
                foreach (var item in values)
                    if (item == null || item.ToString() == string.Empty || item.ToString() == "{DependencyProperty.UnsetValue}") return string.Empty;

                return (Double.Parse(values[0].ToString()) / Double.Parse(values[1].ToString())).ToString("#,##0.0000");
            }
            return string.Empty;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class BoxesPerPalletConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            //BoxesPerPallet = PartsPerPallet / (PartsPerBox > 0 ? PartsPerBox : 1);//residual:calcTTLPackagingPiecePerPart
            if (values.Count() == 2)
            {
                foreach (var item in values)
                    if (item == null || item.ToString() == string.Empty || item.ToString() == "{DependencyProperty.UnsetValue}") return string.Empty;

                if (Int32.Parse(values[1].ToString()) == 0) return string.Empty;

                return (Int32.Parse(values[0].ToString()) / Int32.Parse(values[1].ToString())).ToString("#,##0.0000");
            }
            return string.Empty;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class DyeCostPerPartConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            //DyeCostPerPart = PartWeight / 100 * (DyeMixRatio / 100) * DyeCostPerLB
            if (values.Count() == 3)
            {
                foreach (var item in values)
                    if (item == null || item.ToString() == string.Empty || item.ToString() == "{DependencyProperty.UnsetValue}") return string.Empty;

                return (Double.Parse(values[0].ToString()) / 100 * Double.Parse(values[1].ToString()) * Double.Parse(values[2].ToString())).ToString("#,##0.0000");
            }
            return string.Empty;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class MoldSetupCostConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            //MoldSetupCost = MoldSetupTime * 20;
            if (values.Count() == 1)
            {
                foreach (var item in values)
                    if (item == null || item.ToString() == string.Empty || item.ToString() == "{DependencyProperty.UnsetValue}") return string.Empty;

                return (Double.Parse(values[0].ToString()) * 20).ToString("#,##0.0000");
            }
            return string.Empty;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class PartsPerHourConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            //PartsPerHour = 3600 / TargetCycleTime * PartsPerShot;
            if (values.Count() == 2)
            {
                foreach (var item in values)
                    if (item == null || item.ToString() == string.Empty || item.ToString() == "{DependencyProperty.UnsetValue}") return string.Empty;

                return (3600 / Int32.Parse(values[0].ToString()) * Int32.Parse(values[1].ToString())).ToString("#,##0.0000");
                //PartsPerHour = 60 / actualcycletime / 60 * partspershot
                //Cant do it: Where is ActualCycleTime? Calculate on the fly? Discuss
            }
            return string.Empty;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class NaturalResinConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            //NaturalResin = MaterialType.CostPerLB * PartWeight; 
            if (values.Count() == 2)
            {
                foreach (var item in values)
                    if (item == null || item.ToString() == string.Empty || item.ToString() == "{DependencyProperty.UnsetValue}") return string.Empty;

                return (Double.Parse(values[0].ToString()) * Double.Parse(values[1].ToString())).ToString("#,##0.0000");
            }
            return string.Empty;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class ResinDyePricePerPartConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            //double dResin = ((double)PartWeight - (double)DyeMixRatio) / (100 / (double)PartWeight);
            //ResinDyePricePerPart = (dResin / 100 * DyeMixRatio * DyeCostPerLB) + (MaterialType.CostPerLB * dResin); //residual: calcTTLCostPrePartMaterialPackaging
            if (values.Count() == 4)
            {
                foreach (var item in values)
                    if (item == null || item.ToString() == string.Empty || item.ToString() == "{DependencyProperty.UnsetValue}") return string.Empty;

                double partWeight = double.Parse(values[0].ToString());
                double dyeMixRatio = double.Parse(values[1].ToString());
                double dyeCostPerLB = double.Parse(values[2].ToString());
                double materialTypeCostPerLB = double.Parse(values[3].ToString());

                double dResin = (partWeight - dyeMixRatio) / 100 / partWeight;
                return ((dResin / 100 * dyeMixRatio * dyeCostPerLB) + (materialTypeCostPerLB * dResin)).ToString("#,##0.0000");
            }
            return string.Empty;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class StartupScrapPriceConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            //StartupScrapPrice = (StartupScrap / 100) * DyeMixRatio * DyeCostPerLB + (StartupScrap * MaterialType.CostPerLB);
            if (values.Count() == 4)
            {
                foreach (var item in values)
                    if (item == null || item.ToString() == string.Empty || item.ToString() == "{DependencyProperty.UnsetValue}") return string.Empty;

                return (Int32.Parse(values[0].ToString()) / 100 * Double.Parse(values[1].ToString()) * Double.Parse(values[2].ToString()) +
                    (Int32.Parse(values[0].ToString()) * Double.Parse(values[3].ToString()))).ToString("#,##0.0000");
            }
            return string.Empty;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class PartsPerHour_ASConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            //PartsPerHour_AS = 3600 / AverageCycleTime_AS * PartsPerShot;
            if (values.Count() == 2)
            {
                foreach (var item in values)
                    if (item == null || item.ToString() == string.Empty || item.ToString() == "{DependencyProperty.UnsetValue}") return string.Empty;

                return (3600 / Double.Parse(values[0].ToString()) * Int32.Parse(values[1].ToString())).ToString("#,##0.0000");
            }
            return string.Empty;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class TTLPackagingPiecePerPartConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            //TTLPackagingPiecePerPart = (BoxPrice / PartsPerBox) + (4 / PartsPerPallet) + AQ41Price + (PricePerBox / PartsPerBox);
            if (values.Count() == 5)
            {
                foreach (var item in values)
                    if (item == null || item.ToString() == string.Empty || item.ToString() == "{DependencyProperty.UnsetValue}") return string.Empty;

                Double boxPrice = Double.Parse(values[0].ToString());
                int partsPerBox = Int32.Parse(values[1].ToString());
                if (partsPerBox > 0)
                {
                    int partsPerPallet = Int32.Parse(values[2].ToString());
                    if (partsPerPallet > 0)
                    {
                        Double aq41Price = Double.Parse(values[3].ToString());
                        Double pricePerBox = Double.Parse(values[4].ToString());
                        return ((boxPrice / partsPerBox) + (4 / partsPerPallet) + aq41Price + (pricePerBox / partsPerBox)).ToString("#,##0.0000");
                    }
                }
            }
            return string.Empty;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class TTLCostPrePartMaterialPackagingConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            //TTLCostPrePartMaterialPackaging = (ResinDyePricePerPart + MaterialCost + TTLPackagingPiecePerPart);
            if (values.Count() == 3)
            {
                foreach (var item in values)
                    if (item == null || item.ToString() == string.Empty || item.ToString() == "{DependencyProperty.UnsetValue}") return string.Empty;

                return (Double.Parse(values[0].ToString()) + Double.Parse(values[1].ToString()) + Double.Parse(values[2].ToString())).ToString("#,##0.0000");
            }
            return string.Empty;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class LaborSecondPerPartConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            //LaborSecondPerPart = AverageCycleTime_AS / PartsPerShot;//residual:calcLaborTTLPerPart
            if (values.Count() == 2)
            {
                foreach (var item in values)
                    if (item == null || item.ToString() == string.Empty || item.ToString() == "{DependencyProperty.UnsetValue}") return string.Empty;

                return (Double.Parse(values[0].ToString()) / Int32.Parse(values[1].ToString())).ToString("#,##0.0000");
            }
            return string.Empty;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class LaborTTLPerPartConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            //LaborTTLPerPart = LaborSecondPerPart * 0.0042;//residual:calcTTLPrePartMaterialPackagingLabor, calcAfterSetupRunTarget, calcAfterSetupAvgCycleTime
            if (values.Count() == 1)
            {
                foreach (var item in values)
                    if (item == null || item.ToString() == string.Empty || item.ToString() == "{DependencyProperty.UnsetValue}") return string.Empty;

                return (Double.Parse(values[0].ToString()) * 0.0042).ToString("#,##0.0000");
            }
            return string.Empty;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class OverheadFactorConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            //OverheadFactor = 0.36 / PartsPerShot;
            if (values.Count() == 1)
            {
                foreach (var item in values)
                    if (item == null || item.ToString() == string.Empty || item.ToString() == "{DependencyProperty.UnsetValue}") return string.Empty;

                return (0.36 / Int32.Parse(values[0].ToString())).ToString("#,##0.0000");
            }
            return string.Empty;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class CostStartPressConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            //CostStartPress = MoldSetupCost + StartupScrapPrice;//residual:calcTTLCostPartAverage
            if (values.Count() == 2)
            {
                foreach (var item in values)
                    if (item == null || item.ToString() == string.Empty || item.ToString() == "{DependencyProperty.UnsetValue}") return string.Empty;

                return (Double.Parse(values[0].ToString()) + Double.Parse(values[1].ToString())).ToString("#,##0.0000");
            }
            return string.Empty;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class TTLPrePartMaterialPackagingLaborConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            //TTLPrePartMaterialPackagingLabor = OpCostPerPart + TTLCostPrePartMaterialPackaging + LaborTTLPerPart;
            if (values.Count() == 3)
            {
                foreach (var item in values)
                    if (item == null || item.ToString() == string.Empty || item.ToString() == "{DependencyProperty.UnsetValue}") return string.Empty;

                return (Double.Parse(values[0].ToString()) + Double.Parse(values[1].ToString()) + Double.Parse(values[2].ToString())).ToString("#,##0.0000");
            }
            return string.Empty;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class TTLCostPartAverageConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            //TTLCostPartAverage = CostStartPress / 1000 + TTLPrePartMaterialPackagingLabor;
            if (values.Count() == 2)
            {
                foreach (var item in values)
                    if (item == null || item.ToString() == string.Empty || item.ToString() == "{DependencyProperty.UnsetValue}") return string.Empty;

                return (Double.Parse(values[0].ToString()) / 1000 + Double.Parse(values[1].ToString())).ToString("#,##0.0000");
            }
            return string.Empty;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class TTLCostRunTargetConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            //TTLCostRunTarget = (Price - TTLCostPartAverage) * PartsPerHour;   
            if (values.Count() == 3)
            {
                foreach (var item in values)
                    if (item == null || item.ToString() == string.Empty || item.ToString() == "{DependencyProperty.UnsetValue}") return string.Empty;

                return ((Double.Parse(values[0].ToString()) - Double.Parse(values[1].ToString())) * Int32.Parse(values[2].ToString())).ToString("#,##0.0000");
            }
            return string.Empty;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class TTLCostAvgCycleTimeConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            //TTLCostAvgCycleTime = (Price - TTLCostPartAverage) * PartsPerHour_AS;
            if (values.Count() == 3)
            {
                foreach (var item in values)
                    if (item == null || item.ToString() == string.Empty || item.ToString() == "{DependencyProperty.UnsetValue}") return string.Empty;

                return ((Double.Parse(values[0].ToString()) - Double.Parse(values[1].ToString())) * Double.Parse(values[2].ToString())).ToString("#,##0.0000");
            }
            return string.Empty;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class AfterSetupRunTargetConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            //AfterSetupRunTarget = (Price - TTLCostPartAverage + LaborTTLPerPart + OpCostPerPart) * PartsPerHour;
            if (values.Count() == 5)
            {
                foreach (var item in values)
                    if (item == null || item.ToString() == string.Empty || item.ToString() == "{DependencyProperty.UnsetValue}") return string.Empty;

                return ((Double.Parse(values[0].ToString()) - Double.Parse(values[1].ToString()) + Double.Parse(values[2].ToString()) + Double.Parse(values[3].ToString()))
                    * Int32.Parse(values[4].ToString())).ToString("#,##0.0000");
            }
            return string.Empty;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class AfterSetupAvgCycleTimeConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            //AfterSetupAvgCycleTime = (Price - TTLCostPartAverage + LaborTTLPerPart + OpCostPerPart) * PartsPerHour_AS;
            if (values.Count() == 5)
            {
                foreach (var item in values)
                    if (item == null || item.ToString() == string.Empty || item.ToString() == "{DependencyProperty.UnsetValue}") return string.Empty;

                return ((Double.Parse(values[0].ToString()) - Double.Parse(values[1].ToString())
                    + Double.Parse(values[2].ToString()) + Double.Parse(values[3].ToString())) * Double.Parse(values[4].ToString())).ToString("#,##0.0000");
            }
            return string.Empty;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class AfterMaterialsRunTargetConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            //AfterMaterialsRunTarget = (Price - TTLCostPrePartMaterialPackaging) * PartsPerHour;
            if (values.Count() == 3)
            {
                foreach (var item in values)
                    if (item == null || item.ToString() == string.Empty || item.ToString() == "{DependencyProperty.UnsetValue}") return string.Empty;

                return ((Double.Parse(values[0].ToString()) - Double.Parse(values[1].ToString())) * Int32.Parse(values[2].ToString())).ToString("#,##0.0000");
            }
            return string.Empty;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class AfterMaterialsAvgCycleTimeConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            //AfterMaterialsAvgCycleTime = (Price - TTLCostPrePartMaterialPackaging) * PartsPerHour_AS;
            if (values.Count() == 3)
            {
                foreach (var item in values)
                    if (item == null || item.ToString() == string.Empty || item.ToString() == "{DependencyProperty.UnsetValue}") return string.Empty;

                return ((Double.Parse(values[0].ToString()) - Double.Parse(values[1].ToString())) * Double.Parse(values[2].ToString())).ToString("#,##0.0000");
            }
            return string.Empty;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class BoxesNeededConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            if (values.Count() == 2)
            {
                foreach (var item in values)
                    if (item == null || item.ToString() == string.Empty || item.ToString() == "{DependencyProperty.UnsetValue}") return string.Empty;

                if (Int32.Parse(values[1].ToString()) == 0) return string.Empty;

                // CorrectedQuantity / PartsPerBox
                return Math.Round(Decimal.Parse((Int32.Parse(values[0].ToString()) / Int32.Parse(values[1].ToString())).ToString()),0).ToString("#,##0");
            }
            return string.Empty;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class PalletsNeededConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            //oQuantity / PartsPerPallet Rounded up
            if (values.Count() == 2)
            {
                foreach (var item in values)
                    if (item == null || item.ToString() == string.Empty || item.ToString() == "{DependencyProperty.UnsetValue}") return string.Empty;

                if (Int32.Parse(values[1].ToString()) == 0) return string.Empty;

                return Math.Round(Decimal.Parse((Int32.Parse(values[0].ToString()) / Int32.Parse(values[1].ToString())).ToString()), 0).ToString("#,##0");
            }
            return string.Empty;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class ShotsNeededConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            //Quantity / PartsPerShot
            if (values.Count() == 2)
            {
                foreach (var item in values)
                    if (item == null || item.ToString() == string.Empty || item.ToString() == "{DependencyProperty.UnsetValue}") return string.Empty;

                if (Int32.Parse(values[1].ToString()) == 0) return string.Empty;

                return Math.Round(Decimal.Parse((Int32.Parse(values[0].ToString()) / Int32.Parse(values[1].ToString())).ToString()), 0).ToString("#,##0");
            }
            return string.Empty;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
